package dump;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;

public class ContactMethod {
	public static class ContactMethodsCollection extends DatabaseCollection {
		public ContactMethodsCollection(final Cursor cursor) {
			super(cursor);
		}

		@Override
		public ContentValues cursorToValues(final Cursor cursor) {
			if (cursor == null) {
				return null;
			}

			final ContentValues values = new ContentValues();
			String val;
			val = cursor.getString(cursor
					.getColumnIndex(android.provider.BaseColumns._ID));
			values.put(android.provider.BaseColumns._ID, val);
			val = cursor.getString(cursor
					.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
			values.put(Contacts.ContactMethodsColumns.DATA, val);
			val = cursor.getString(cursor
					.getColumnIndex(Contacts.ContactMethodsColumns.AUX_DATA));
			values.put(Contacts.ContactMethodsColumns.AUX_DATA, val);
			val = cursor.getString(cursor
					.getColumnIndex(Contacts.ContactMethodsColumns.LABEL));
			values.put(Contacts.ContactMethodsColumns.LABEL, val);
			Integer intVal = new Integer(cursor.getInt(cursor
					.getColumnIndex(Contacts.ContactMethodsColumns.ISPRIMARY)));
			values.put(Contacts.ContactMethodsColumns.ISPRIMARY, intVal);
			intVal = new Integer(cursor.getString(cursor
					.getColumnIndex(Contacts.ContactMethodsColumns.KIND)));
			values.put(Contacts.ContactMethodsColumns.KIND, intVal);
			intVal = new Integer(cursor.getString(cursor
					.getColumnIndex(Contacts.ContactMethodsColumns.TYPE)));
			values.put(Contacts.ContactMethodsColumns.TYPE, intVal);
			return values;
		}
	}

	static final String[] contactMethodsProjection = new String[] {
			android.provider.BaseColumns._ID,
			android.provider.Contacts.ContactMethodsColumns.DATA,
			android.provider.Contacts.ContactMethodsColumns.AUX_DATA,
			android.provider.Contacts.ContactMethodsColumns.KIND,
			android.provider.Contacts.ContactMethodsColumns.LABEL,
			android.provider.Contacts.ContactMethodsColumns.TYPE,
			android.provider.Contacts.ContactMethodsColumns.ISPRIMARY };

	public static void addContactMethod(final ContentResolver resolver,
			final ContentValues contactMethod, final String userId) {
		final Uri peopleUri = Uri.withAppendedPath(Contacts.People.CONTENT_URI,
				userId);
		resolver.insert(Uri.withAppendedPath(peopleUri,
				Contacts.People.ContactMethods.CONTENT_DIRECTORY),
				contactMethod);
	}

	public static void deleteContactMethod(final ContentResolver resolver,
			final String id, final String userId) {
		resolver.delete(Uri.withAppendedPath(
				Contacts.ContactMethods.CONTENT_URI, id), null, null);
	}

	/**
	 * getContactMethods
	 * 
	 * Get the ContactMethods for a Contact.
	 * 
	 * @param resolver
	 * @param id
	 * @return
	 */
	public static ContactMethodsCollection getContactMethods(
			final ContentResolver resolver, final String userId) {
		if (userId == null) {
			return null;
		}

		final String[] whereArgs = new String[] { userId };
		return new ContactMethodsCollection(resolver.query(
				Contacts.ContactMethods.CONTENT_URI,
				ContactMethod.contactMethodsProjection, "people."
						+ android.provider.BaseColumns._ID + " = ?", whereArgs,
				Contacts.ContactMethodsColumns.KIND + " DESC"));
	}

	public static void saveContactMethod(final ContentResolver resolver,
			final ContentValues contactMethod, final String id,
			final String userId) {
		final Uri uri = Uri.withAppendedPath(
				Contacts.ContactMethods.CONTENT_URI, id);
		resolver.update(uri, contactMethod, null, null);
	}
}
